package com.shengyin.ec.service.mail;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shengyin.ec.model.dao.mail.MailDao;
import com.shengyin.ec.model.entity.Mail;

@Service(value ="mailService")
public class MailServiceImpl implements MailService{

	@Autowired
	MailDao mailDao;
	
	
	public List<Mail> getMailList(){
		List<Mail> mailList = mailDao.loadAll();
		return mailList;
	}


	@Override
	public void createMail(Mail mail) {
	      mailDao.save(mail);
	}


	@Override
	public void removeMailById(long id) {
		mailDao.deleteByKey(id);
	}


	@Override
	public void modifyMail(Mail mail) {
		
		Mail mail1 = mailDao.get(mail.getId());
		try {
			BeanUtils.copyProperties(mail1, mail);
			mailDao.update(mail1);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public Mail getMail(long id){
		Mail mail = mailDao.get(id);
		
		return mail;
	}


	@Override
	public void deleteAllMail(String[] ids) {
		
	}


	@Override
	public List<Mail> getMailListByDate() {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public void deleteSelectedMail(String[] ids) {
		if(ids!=null){
		Long[] idsl=new Long[ids.length];
		for(int i=0;i<ids.length;i++){
			if(ids[i]!=null||!ids[i].isEmpty()){
				idsl[i]=Long.valueOf(ids[i].trim());
			}
			
		}
		mailDao.deleteSelectedMail(idsl);
		}
	}


	@Override
	public List<Mail> getMailList(int offset, int pagesize) {
		return mailDao.getMailList(offset, pagesize);
	}


	@Override
	public Long getTotal() {
		return mailDao.getTotal();
	}
}
